﻿using Abp.Dependency;
using GN.Pay.Core.Exceptions;
using GN.Pay.Core.Services;
using GN.Pay.Core.Values.Entities;
using GN.Pay.Utils.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GN.Pay.TradeChannel;

namespace GN.Pay.Core.Runtime.Impl
{
    /// <summary>
    /// 
    /// </summary>
    public class GenerateIdentifier : IGenerateIdentifier
    {
        private readonly IIocManager iocManager;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="iocManager"></param>
        public GenerateIdentifier(IIocManager iocManager)
        {
            this.iocManager = iocManager;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="merchantId"></param>
        /// <param name="appName"></param>
        /// <returns></returns>
        public string GenerateAppId(string merchantId, string appName)
        {
            return "gn" + merchantId.Substring(4, 4) + DateTime.Now.ToString("ddmmffffffHHssMM");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="merchantName"></param>
        /// <param name="merchantType"></param>
        /// <returns></returns>
        public string GenerateMerchantId(string merchantName, int merchantType)
        {
            if (merchantType == 1)
            {
                return "6088" + DateTime.Now.ToString("ffffffHHssMMddyyyymm");
            }
            else if (merchantType == 2)
            {
                return "5088" + DateTime.Now.ToString("ffffffHHssMMddyyyymm");
            }
            return "4088" + DateTime.Now.ToString("ffffffHHssMMddyyyymm");
        }

        /// <summary>
        /// 生成会员Id
        /// </summary>
        /// <param name="merchantId">商户Id</param>
        /// <param name="memberType">会员类型(1=个人,2=企业)</param>
        /// <param name="sourceType">来源类型(1=自行注册,2=应用注册)</param>
        /// <returns></returns>
        public string GenerateMemberId(string merchantId, int memberType, int sourceType)
        {
            StringBuilder builder = new StringBuilder();
            switch (memberType)
            {
                case 2:
                    builder.Append("13");
                    break;
                default:
                    builder.Append("16");
                    break;
            }
            switch (sourceType)
            {
                case 2:
                    builder.Append("23");
                    break;
                default:
                    builder.Append("26");
                    break;
            }
            builder.Append(DateTime.Now.ToString("ffffffMMddmmHHyyyyss"));
            return builder.ToString();
        }

        private const string NumberString = "1234567890";

        private const string BlendString = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        private string CreateCheckCode(bool numberFlag, int length)
        {
            StringBuilder sb = new StringBuilder();
            string strTable = numberFlag ? NumberString : BlendString;
            int len = strTable.Length;
            Random random = new Random();
            for (int i = 0; i < length; i++)
            {
                var index = random.Next(0, len - 1);
                sb.Append(strTable[index]);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 生成短信验证码
        /// </summary>
        /// <returns></returns>
        public string GenerateSmsCheckCode()
        {
            return CreateCheckCode(true, 6);
        }

        /// <summary>
        /// 生成应用希哈短验证码
        /// </summary>
        /// <param name="appId">应用</param>
        /// <param name="checkCode">短信验证码</param>
        /// <returns></returns>
        public string GenerateAppHashSmsCheckCode(string appId, string checkCode)
        {
            var service = iocManager.Resolve<ITradeCacheService>();
            var app = service.GetMerchantAppInfo(appId);
            return GenerateAppHashSmsCheckCode(app, checkCode);
        }

        /// <summary>
        /// 生成应用通道希哈短验证码
        /// </summary>
        /// <param name="appId">应用</param>
        /// <param name="checkCode">短信验证码</param>
        /// <returns></returns>
        public string GenerateAppHashSmsCheckCode(MerchantAppCahceInfo app, string checkCode)
        {
            if (app == null)
            {
                PayExceptionUtils.ThrowPayValidationException("无效的应用或应用已下架");
            }
            var content = string.Format("appId={0}&merchantId={1}&checkCode={2}&key={3}", app.Id, app.MerchantId, checkCode, app.SignKey);
            return Md5Utils.Sign(content, Encoding.UTF8);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applyId"></param>
        /// <param name="app"></param>
        /// <param name="apply"></param>
        /// <param name="checkCode"></param>
        /// <returns></returns>
        public string GeneratePayApplyHashSmsCheckCode(string applyId,
            MerchantAppCahceInfo app, IOrderPayApply apply, string checkCode)
        {
            if (apply == null)
            {
                PayExceptionUtils.ThrowPayValidationException("申请信息为 null");
            }
            if (app == null)
            {
                PayExceptionUtils.ThrowPayValidationException("应用为 null");
            }
            var service = iocManager.Resolve<ITradeCacheService>();
            var content = string.Format("appId={0}&orderNo={1}&orderMoney={2}&checkCode={3}&key={4}", app.Id, apply.OrderNo, apply.OrderMoney.ToString("N2"), checkCode, app.SignKey);
            return Md5Utils.Sign(content, Encoding.UTF8);
        }
    }
}
